﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace Mengsk.ERP.App.Number
{
    /// <summary>
    /// NumberUserControl.xaml 的交互逻辑
    /// </summary>
    public partial class NumberUserControl : UserControl, IUIPlugin
    {
        private string mark = "ZABCDEFGHIJKLMN";

        public NumberUserControl()
        {
            InitializeComponent();
        }


        public string Title
        {
            get { return "商品编号"; }
        }

        public Control UIControl
        {
            get { return this; }
        }

        private void tbOriginNumber_PreviewKeyDown(object sender, KeyEventArgs e)
        {
            if (e.Key != Key.Enter)
            {
                return;
            }

            e.Handled = true;

            string number = this.tbOriginNumber.Text.Trim();
            if (string.IsNullOrWhiteSpace(number))
            {
                this.tbNewNumber.Text = "";
                return;
            }

            string rev = string.Join("", number.Reverse());
            char rev0 = rev[0];

            if (rev0 >= '0' && rev0 <= '9')
            {
                rev0 = mark[int.Parse(rev0.ToString())];
            }

            this.tbNewNumber.Text = string.Format("M{0}-{1}", string.Join("", this.tbOriginManufactor.Text.Trim().Reverse()), rev0 + rev.Substring(1));
        }

        private void tbNewNumber_PreviewKeyDown(object sender, KeyEventArgs e)
        {
            if (e.Key != Key.Enter)
            {
                return;
            }
            e.Handled = true;

            try
            {
                string text = this.tbNewNumber.Text.Trim();
                if (string.IsNullOrWhiteSpace(text))
                {
                    this.tbOriginNumber.Text = "";
                    return;
                }

                if (text[0] == 'M' || text[0] == 'm')
                {
                    text = text.Substring(1);
                }

                if (string.IsNullOrWhiteSpace(text))
                {
                    this.tbOriginNumber.Text = "";
                    return;
                }

                //提取厂商
                this.tbOriginManufactor.Text = string.Join("", text.Substring(0, text.IndexOf('-')).Reverse());
                text = text.Substring(text.IndexOf('-') + 1);
                //反向
                text = string.Join("", text.Reverse());

                //最后一个还原
                int index = mark.IndexOf(text[text.Length - 1]);
                if (index >= 0)
                {
                    text = text.Substring(0, text.Length - 1) + index.ToString();
                }
                this.tbOriginNumber.Text = text;
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }
    }
}
